import { SyncHook, AsyncSeriesHook } from 'tapable';

interface EventData {
  [key: string]: any;
}

class PluginEventManager {
  private syncHook: SyncHook<EventData>;
  private asyncHook: AsyncSeriesHook<EventData>;

  constructor() {
    this.syncHook = new SyncHook(['data']);
    this.asyncHook = new AsyncSeriesHook(['data']);
  }

  // 注册同步事件监听器
  onSync(listener: (data: EventData) => void): void {
    this.syncHook.tap('syncEvent', listener);
  }

  // 触发同步事件
  emitSync(data: EventData): void {
    this.syncHook.call(data);
  }

  // 注册异步事件监听器
  onAsync(listener: (data: EventData) => Promise<void>): void {
    this.asyncHook.tapPromise('asyncEvent', listener as unknown as (...args: any[]) => Promise<any>);
  }

  // 触发异步事件
  async emitAsync(data: EventData): Promise<void> {
    await this.asyncHook.promise(data);
  }
}

export default new PluginEventManager();
